package com.kehao.account_book.service;

import com.kehao.account_book.pojo.User;
import com.kehao.account_book.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @author kehao
 * @date 2020/2/22-11:28
 */
@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public boolean addUser(User user){
        List<User> list = userRepository.findByUsername(user.getUsername());
        if (list.size()==0){
            userRepository.save(user);
            return true;
        }else {
            return false;
        }
    }

    public boolean checkPassword(User user){
        List<User> list = userRepository.findByUsername(user.getUsername());
        if (list.size()==0){
            return false;
        }else {
            User userGet = list.get(0);
            if(user.getPassword().equals(userGet.getPassword())){
                return true;
            }else {
                return false;
            }
        }
    }

    @Transactional
    public boolean changePassword(User user){
        List<User> list = userRepository.findByUsername(user.getUsername());
        if (list.size()==0){
            return false;
        }else {
            userRepository.updateUserPassword(user.getPassword(),user.getUsername());
            return true;
        }
    }
}
